using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using OpenCover.Framework.Model;
using UnityEditor;
using UnityEngine;
using File = System.IO.File;

namespace GameManager.Editor
{
    public class PacketHandlerEditor
    {
        [MenuItem("Tools/生成Packet和PacketHandler")]
        private static void GenPacketExt()
        {
            //根据proto类反射生成Packet类和PacketHandler类

            Type packetType = typeof(ProtoBuf.IExtensible);
            Assembly assembly = Assembly.Load("Assembly-CSharp");
            Type[] types = assembly.GetTypes();

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("// Auto Generaed ");
            sb.AppendLine("namespace GameManager.Network");
            sb.AppendLine("{");
            
            foreach (var type in types)
            {
                if (typeof(ProtoBuf.IExtensible).IsAssignableFrom(type))
                {
                    string typenames  = type.FullName.Split('.')[1];
                    string[] SplitNames = type.FullName.Split('_');
                    string EventId = SplitNames[SplitNames.Length-1];
                    sb.AppendLine($"    public partial class {typenames} : {GetBaseClass(typenames)}");
                    sb.AppendLine("    {");
                    sb.AppendLine($"        public override int Id => {EventId};");
                    sb.AppendLine("    }");
                    sb.AppendLine();
                    if (typenames.StartsWith("SC"))
                    {
                        sb.AppendLine(
                            $"    public partial class {typenames}_Handler : GameManager.Network.PacketHandlerBase");
                        sb.AppendLine("    {");
                        sb.AppendLine($"        public override int Id => {EventId};");
                        sb.AppendLine("    }");
                        sb.AppendLine();
                    }
                }
            }

            sb.AppendLine("}");
            File.WriteAllText($"Assets/Scripts/Network/Packet/PacketExt.cs",sb.ToString());
            GenPacketHandler();
            AssetDatabase.Refresh();
        }

        private static void GenPacketHandler()
        {
            //根据proto类反射生成Packet类和PacketHandler类

            Type packetType = typeof(ProtoBuf.IExtensible);
            Assembly assembly = Assembly.Load("Assembly-CSharp");
            Type[] types = assembly.GetTypes();

            
            
            foreach (var type in types)
            {
                if (typeof(ProtoBuf.IExtensible).IsAssignableFrom(type))
                {
                    StringBuilder sb = new StringBuilder();
                    string typenames  = type.FullName.Split('.')[1];
                    if(!typenames.StartsWith("SC"))
                        continue;
                    if(File.Exists($"{typenames}_Handler"))
                        continue;
                    sb.AppendLine("using EasyNetWork;");
                    sb.AppendLine("namespace GameManager.Network");
                    sb.AppendLine("{");
                    sb.AppendLine(
                        $"    public partial class {typenames}_Handler : GameManager.Network.PacketHandlerBase");
                    sb.AppendLine("    {");
                    sb.AppendLine($"        public override void Handle(object sender,Packet packet)");
                    sb.AppendLine("        {");
                    sb.AppendLine("        }");
                    sb.AppendLine("    }");
                    sb.AppendLine();
                    sb.AppendLine("}");
                    
                    File.WriteAllText($"Assets/Scripts/Network/PacketHandler/{typenames}_Handler.cs",sb.ToString());
                }
            }
           
            AssetDatabase.Refresh();
        }

        private static string GetBaseClass(string typeName)
        {
            if (typeName.StartsWith("CS"))
            {
                return "CSPacketBase";
            }
            else if (typeName.StartsWith("SC"))
            {
                return "SCPacketBase";
            }

            return null;
        }
    }

}
